﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.AspNetCore.Mvc;
using Web.Client.Services.Orders;
using XCore.Authorization;
using XCore.Extensions;
using SaaS.Model.Enums;

namespace Web.Client.Controllers
{
    /// <summary>
    /// 订单中心
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class OrderController : ClientControllerBase
    {
        private readonly OrderAppService _orderApplicationService;
        /// <summary>
        /// 构造
        /// </summary>
        /// <param name="orderApplicationService"></param>
        public OrderController(OrderAppService orderApplicationService)
        {
            _orderApplicationService = orderApplicationService;
        }
        /// <summary>
        /// 查询用户订单
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost, Permission]
        public ActionResult List()
        {
            var query = SCC.Orders.Where(c => c.UserId == UserId.Value && c.Status != OrderStatus.Delete);
            return PageResult(query.OrderByDescending(c => c.CreationTime), c => new
            {
                c.Id,
                c.ProjectName,
                c.TotalPrice,
                PayType = c.PayType.GetDescription(),
                Status = c.Status.GetDescription(),
                CreationTime = c.CreationTime.ToString("yyyy-MM-dd HH:mm:ss")
            });
        }
        /// <summary>
        /// 保存订单
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost, Permission]
        public ActionResult Save(int id)
        {
            return Ok(_orderApplicationService.CreateAsync(UserId.Value, id));
        }
        /// <summary>
        /// 删除订单
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete, Permission]
        public ActionResult Delete(int id)
        {
            return Ok(_orderApplicationService.Delete(id, UserId.Value));
        }
        /// <summary>
        /// 发起支付
        /// </summary>
        /// <returns></returns>
        [HttpPost, Permission]
        public ActionResult Pay()
        {
            return Ok("回调地址");
        }
    }
}